package org.dromara.shopping.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.shopping.domain.Organ;
import org.dromara.shopping.service.IOrganService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.shopping.domain.Mer;
import org.dromara.shopping.service.IMerService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 服务商家
 *
 * @author Mr.Zhou
 * @date 2025-01-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/shopping/mer")
public class MerController extends BaseController {

    private final IMerService merService;
    private final IOrganService organService;

    /**
     * 查询服务商家列表
     */
    @SaCheckPermission("shopping:mer:list")
    @GetMapping("/list")
    public TableDataInfo<Mer> list(Mer mer, PageQuery pageQuery) {
        QueryWrapper<Mer> qw = new QueryWrapper<>();
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getRoleKey().equals("organ")){
            qw.eq("organ_id", loginUser.getOrganId());
        }

        TableDataInfo<Mer> td = PageUtils.buildDataInfo(merService.page(PageUtils.buildPage(),qw));
        List<Mer> list = td.getRows();
        return td;
    }

    /**
     * 导出服务商家列表
     */
    @SaCheckPermission("shopping:mer:export")
    @Log(title = "服务商家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(Mer mer, HttpServletResponse response) {
        List<Mer> list = merService.list();
        ExcelUtil.exportExcel(list, "服务商家", Mer.class, response);
    }

    /**
     * 获取服务商家详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("shopping:mer:query")
    @GetMapping("/{id}")
    public R<Mer> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        Mer mer = merService.getById(id);
        if(mer != null){

        }
        return R.ok(mer);
    }

    /**
     * 新增服务商家
     */
    @SaCheckPermission("shopping:mer:add")
    @Log(title = "服务商家", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Mer mer) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getRoleKey().equals("organ")){
            mer.setOrganId(loginUser.getOrganId());
        }
        mer.setCreateTime(new Date());
        return toAjax(merService.save(mer));
    }


    /**
     * 新增服务商家
     */
    @Log(title = "商家移动端注册", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/register")
    public R<Void> register(@Validated(AddGroup.class) @RequestBody Mer mer) {
        QueryWrapper<Organ> qw = new QueryWrapper<>();
        qw.eq("organ_no", mer.getOrganNo());
        qw.last("limit 1");
        Organ organ = organService.getOne(qw);
        if(organ !=null){
            mer.setCheckState(0);
            mer.setCreateTime(new Date());
            return toAjax(merService.save(mer));
        }
        return R.fail("未归属到机构，请扫码注册");
    }

    /**
     * 修改服务商家
     */
    @SaCheckPermission("shopping:mer:edit")
    @Log(title = "服务商家", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Mer mer) {
        return toAjax(merService.updateById(mer));
    }

    /**
     * 删除服务商家
     *
     * @param ids 主键串
     */
    @SaCheckPermission("shopping:mer:remove")
    @Log(title = "服务商家", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(merService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
